package com.jy.springbootcmsservice.common.base.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.jy.springbootcmsservice.common.constant.HttpStatus;
import lombok.Data;

import javax.servlet.http.HttpServletResponse;

/**
 *  基础返回类
 * @author Administrator
 */
@Data
public class BaseResult {

    /**
     * 数据
     */
    private Object data;
    /**
     * 信息
     */
    private String msg;
    /**
     * 错误详情
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String error;

    public BaseResult() {
    }

    /**
     * 创建一个成功返回
     * @param data
     * @return
     */
    public static BaseResult createSuccess(Object data){
        return createSuccess(data, "操作成功");
    }

    /**
     * 创建一个成功返回
     * @param data
     * @param msg
     * @return
     */
    public static BaseResult createSuccess(Object data, String msg){
        BaseResult result = new BaseResult();
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    /**
     * 操作成功
     * @param data
     * @param msg
     * @return
     */
    public void success(Object data, String msg){
        this.data = data;
        this.msg = msg;
    }

    /**
     * 操作失败
     * @param response
     * @param errorMsg
     */
    public void error(HttpServletResponse response, String errorMsg){
        response.setStatus(HttpStatus.STATUS_400);
        error = errorMsg;
    }

}
